<?php

namespace app\console\controller;



use app\console\model\Job as FeedbackTypeModel;

use think\Request;

use think\Url;

use think\Db;

use think\Controller;

use think\Response;

use think\Loader;



class Job extends Controller

{ 

    /*

     * 列表

     */

    public function index()

    {

        // 获取参数

        $inquire = Request::instance()->param();
		


        // 搜索条件

        $condition['id'] = ['gt', 0];

        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];

        if (!empty($inquire['shop_id'])) $condition['shop_id'] = ['eq', $inquire['shop_id']];



        // 读取参数

        $data = FeedbackTypeModel::where($condition)->order(['id' => 'asc', 'id' => 'asc'])->paginate(20, false, ['query' => $inquire]);



        return $this->fetch('index', [

            'mate_title' => '反馈分类列表',

            'data'       => $data

        ]);

    }





    /*

     * 添加

     */

    public function create()

    {



        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();



            $validate = Loader::validate('FeedbackType');

            //if (!$validate->scene('create')->check($data)) fail($validate->getError());



            // 提交数据

            $dispose = (new FeedbackTypeModel())->allowField(true)->save($data);



            if ($dispose) {

                wingo('操作成功', Url::build('index'));

            }

            fail('操作失败');

        }

        return $this->fetch('create', [

            "mate_title" => '反馈分类添加',

        ]);



    }



    /*

     * 删除

     */

    public function destroy($id)

    {



        if (FeedbackTypeModel::destroy($id)) {
			$finance_pay_cart = Db::name('post_list')->where(['post_id' => $id,])->delete();
            winfo('操作成功', Url::build('index'));

        }



        fail('操作失败');



    }



    /*

     * 编辑

     */

    public function update($id)

    {



        if (Request::instance()->isPost()) {



            $data = Request::instance()->param();



            // 验证

            $validate = Loader::validate('FeedbackType');

           // if (!$validate->scene('create')->check($data)) fail($validate->getError());



            // 提交数据

            $dispose = (new FeedbackTypeModel())->allowField(true)->save($data, ['id' => $id]);



            if ($dispose !== false) {

                wingo('操作成功', Url::build('index'));

            }



            fail('操作失败');



        }



        $item = FeedbackTypeModel::get($id);



        return $this->fetch('create', [

            "mate_title" => '反馈分类编辑',

            'item'       => $item



        ]);

    }



    /*

     * 启用状态改变

     */

    public function status()

    {

        $id = Request::instance()->param('id');

        $status = FeedbackTypeModel::where('id', $id)->value('status') == '1' ? 2 : 1;

        if (FeedbackTypeModel::where('id', $id)->update(['status' => $status])) {

            return $status;

        }

        return false;

    }



    /*

     * 排列顺序

     */

    public function sorting()

    {



        $id = Request::instance()->param('orderId/a');

        $sort = Request::instance()->param('sort/a');



        foreach ($id as $key => $value) {

            FeedbackTypeModel::where('id', $value)->update(['sort' => $sort[$key]]);

        }



        win('操作成功', Url::build('FeedbackType/index'));

    }

}

